package template.page;

import java.text.DateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;

import org.apache.click.control.Label;
import org.apache.click.extras.control.Menu;
import org.apache.click.extras.control.MenuFactory;

import cvcg.dto.Top5DTO;
import cvcg.model.AttendingPlayer;
import cvcg.model.Game;
import cvcg.model.GameSession;
import cvcg.model.Vote;
import cvcg.service.GameSessionService;

public class BorderPage extends BasePage {

    private static final long serialVersionUID = 1L;

    private Menu rootMenu;
    protected GameSessionService gameSessionService;
    private Label sessionDateLabel = new Label("sessionDate");
    private Label participantsLabel = new Label("participants");
    private Label votesLabel = new Label("votes");
    private Label top5Label = new Label("top5");
    
	/**
	 * @param gameSessionService setter
	 */
	public void setGameSessionService(GameSessionService gameSessionService) {
		this.gameSessionService = gameSessionService;
	}
	
	/**
	 * @return getter
	 */
	public GameSessionService getGameSessionService() {
		return gameSessionService;
	}

   
    public BorderPage() {
        MenuFactory menuFactory = new MenuFactory();
        rootMenu = menuFactory.getRootMenu();
        addControl(rootMenu); 
        addControl(sessionDateLabel);
        addControl(participantsLabel);
        addControl(votesLabel);
        addControl(top5Label);
    }

    /**
     * @see #getTemplate()
     */
    public String getTemplate() {
        return "/border-template.htm";
    }
    
	@Override
	public void onRender() 
	{
        GameSession nextGameSession = gameSessionService.getNextGameSession();        
        if (nextGameSession!=null) 
        {
        	// Date        	
	        DateFormat df = DateFormat.getDateInstance();
	        sessionDateLabel.setLabel("<STRONG>"+df.format(nextGameSession.getDate())+"</STRONG>");
	        // Player List
	        SortedSet<AttendingPlayer> attendingPlayerList = nextGameSession.getAttendingPlayers();
	        StringBuilder sb = new StringBuilder();
	        for (AttendingPlayer attendingPlayer : attendingPlayerList) 
	        {
				sb.append(attendingPlayer.getPlayerName());
				sb.append(", ");
			}
	        if (sb.length()>2) 
	        {
	        	sb.delete(sb.length()-2, sb.length()-1);
	        }
	        
	        participantsLabel.setLabel(sb.toString());	        
	        // Votes
	        SortedMap<String,Integer> voteMap = new TreeMap<String,Integer>();
	        Map<String,Game> gameMap = new HashMap<String,Game>();
	        
	        Set<Vote> voteList = nextGameSession.getVotes();
	        Game game;
	        String gameName;
	        Integer value;
	        for (Vote vote : voteList) 
	        {
	        	gameName = vote.getGameName();
	        	game = vote.getGame();
	        	gameMap.put(gameName, game);
	        	if (voteMap.containsKey(gameName)) 
	        	{
	        		value = voteMap.get(gameName)+ 1;	        		
	        	} else {
	        		value = Integer.valueOf(1);
	        	}
	        	voteMap.put(gameName,value);
			}
	        StringBuilder sb2 = new StringBuilder();
	        String icon = null;
	        for (String name : voteMap.keySet()) 
	        {       	        	
	        	value = voteMap.get(name);
	        	icon = getDurationIcon(gameMap.get(name));
	        	if (value.compareTo(Integer.valueOf(1))>0) 
	        	{
	        		sb2.append("<li>"+icon+name+" ("+value+")</li>");
	        	}
	        	else {
	        		sb2.append("<li>"+icon+name+"</li>");
	        	}
	        }
	        votesLabel.setLabel(sb2.toString());
	        List<Top5DTO> top5Winner = nextGameSession.getTop5Winner();
	        StringBuilder sb3 = new StringBuilder();
	        if (GameSession.STATUS_CLOSED.equals(nextGameSession.getStatus()))
	        {
		        int i = 0;
		        for (Top5DTO top5 : top5Winner) 
		        {
		        	i++;
		        	sb3.append("<li>"+i+". "+top5.getGameName()+" ("+top5.getPoints()+")</li>");
				}
	        } else {
	        	sb3.append("in progress...");
	        }
		    top5Label.setLabel(sb3.toString());
	        
        } else {
        	sessionDateLabel.setLabel("N/A");
        	participantsLabel.setLabel("N/A");	
        	votesLabel.setLabel("N/A");
        	top5Label.setLabel("N/A");
        		
        }
		super.onRender();
	}
	
	private String getDurationIcon(Game game) 
	{
		String decorator="<IMG style=\"margin-bottom:-3px;padding-right:5px;\" alt=\"%s\" src=\"../assets/images/_%s_16x16.png\"/>";	
		if (game!=null) 
		{
			if(game.isHeavyGame()) {
				decorator = String.format(decorator,"Heavy","Weight"); 
			} else if (game.isNormalGame()) 
			{
				decorator = String.format(decorator,"Normal","beer");
			} else {
				decorator = String.format(decorator,"Short","cocktail");
			}
		}
		return decorator;	
	}
    
    
}
